﻿

#region Using directives
using System;
using System.Web.UI.WebControls;
using InvoiceChecking.Data;
using InvoiceChecking.Entities;
using InvoiceChecking.Web.UI;
#endregion

public partial class Admin_AllInvoice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FormUtil.SetPageIndex(GridView1, "page");
        FormUtil.SetDefaultButton((Button)GridViewSearchPanel1.FindControl("cmdSearch"));
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "UpdatePaymentMode")
        {
            string vatInvoiceNumber = e.CommandArgument.ToString();
            DailyTrans dailyTrans = DataRepository.DailyTransProvider.GetByVatNumberCmsNumber(vatInvoiceNumber,
                                                                                             vatInvoiceNumber.Substring(
                                                                                                 5, 5));
            if (dailyTrans == null)
            {
                DataRepository.DailyTransProvider.CreateInvoice(vatInvoiceNumber);
                dailyTrans = DataRepository.DailyTransProvider.GetByVatNumberCmsNumber(vatInvoiceNumber,
                                                                                             vatInvoiceNumber.Substring(
                                                                                                 5, 5));
            }
            GridViewRow grvRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
            EntityDropDownList ddl_RevisePaymentMode = (EntityDropDownList)grvRow.FindControl("ddl_RevisePaymentMode");
            EntityDropDownList ddl_VoidType = (EntityDropDownList)grvRow.FindControl("ddl_VoidType");
            if (ddl_RevisePaymentMode.SelectedIndex > 0 | (ddl_VoidType.SelectedIndex > 0 | !String.IsNullOrEmpty(dailyTrans.VoidType)))
            {
                DateTime? dateVisit = Convert.ToDateTime(grvRow.Cells[6].Text);
                DataRepository.DailyTransProvider.UpdatePaymentMode(vatInvoiceNumber.Substring(5, 5), dateVisit,
                                                                    ddl_RevisePaymentMode.SelectedValue, ddl_VoidType.SelectedItem.Text);
            }
            TextBox tb_Remark = (TextBox)grvRow.FindControl("tb_Remark");
            if (dailyTrans.Remark != tb_Remark.Text)
            {
                dailyTrans.Remark = tb_Remark.Text;
                dailyTrans.PmntModeRevised = ddl_RevisePaymentMode.SelectedValue;
                dailyTrans.VoidType = ddl_VoidType.SelectedItem.Text;
                DataRepository.DailyTransProvider.Update(dailyTrans);
            }
        }
    }
}


